package com.clock.controller.stage;

import com.clock.common.domain.R;
import com.clock.domain.entity.Tag;
import com.clock.domain.entity.TagModule;
import com.clock.domain.vo.TagVo;
import com.clock.service.TagModuleService;
import com.clock.service.TagService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = {"标签接口"})
@RestController
@RequestMapping("/tag")
public class TagController {

    @Resource
    TagService tagService;

    @Resource
    TagModuleService tagModuleService;


    @ApiOperation(value = "获得所有标签", notes = "")
    @GetMapping("getAllTag")
    public R getAllTag(){
        List<TagVo> allTag = tagService.getAllTag();
        if (allTag != null) {
            return R.ok(allTag);
        }
        return R.fail("获取失败");
    }

    @ApiOperation(value = "新增标签", notes = "只需要模块ID 和 tag内容")
    @PostMapping("/addTag")
    public R addTag(@RequestBody @Validated Tag tag){
        Boolean b = tagService.insertTag(tag);
        if (!b) {
            return R.fail("标签已存在,也有可能是网络开小差了哦");
        }
        return R.ok("标签添加成功");
    }

    @ApiOperation(value = "删除标签", notes = "只允许管理员和顶级管理员访问,通过标签内容和模块ID来删除")
    @PostMapping("/deleteTag")
    public R deleteTag(@RequestBody @Validated Tag tag){
        boolean deleteTag = tagService.deleteTag(tag);
        if (!deleteTag) {
            return R.fail("删除标签失败");
        }
        return R.ok("删除标签成功");
    }

    @ApiOperation(value = "获得所有的标签模块", notes = "")
    @GetMapping("getAllTagModule")
    public R getAllTagModule(){
        List<TagModule> allTagModule = tagModuleService.getAllTagModule();
        if (allTagModule.isEmpty()) {
            return R.fail("获取标签模块失败");
        }
        return R.ok(allTagModule);
    }

}
